<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>详情</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
          name="viewport">
    <link href="../../layuiadmin/layui/css/layui.css" media="all" rel="stylesheet">
</head>
<body>

<div class="layui-form" id="layuiadmin-app-form-list" lay-filter="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <input id="entityId" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">客房号</label>
        <div class="layui-input-inline">
            <input autocomplete="off" class="layui-input" id="rname" lay-verify="required" placeholder="请输入客房号"
                   type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">客房类型</label>
            <div class="layui-input-block">
                <select id="roomTypeId" lay-filter="roomTypeId">
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">面积(m²)</label>
        <div class="layui-input-inline">
            <input autocomplete="off" class="layui-input" id="rsize" lay-verify="required|mm2" placeholder="请输入客房面积"
                   type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">房间朝向</label>
        <div class="layui-input-inline">
            <input autocomplete="off" class="layui-input" id="rtoword" lay-verify="required" placeholder="请输入客房朝向"
                   type="text">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input id="entity-submit" lay-filter="entity-submit" lay-submit type="button"
               value="确认添加">
        <input id="layuiadmin-app-form-edit" lay-filter="layuiadmin-app-form-edit" lay-submit type="button"
               value="确认编辑">
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.min.js"></script>
<script>

    //下拉框初始化
    $.ajax({
        url: '/room/room_type',
        type: 'get',
        dataType: 'json',
        data: {},
        async: false,//设置阻断
        success: function (data) {

            //将下拉框中添加数据
            var modelList = data.roomtype;
            if (modelList && modelList.length != 0) {

                for (var i = 0; i < modelList.length; i++) {
                    var option = "<option value=\"" + modelList[i].id + "\"";
                    option += ">" + modelList[i].name + "</option>";  //动态添加数据
                    $('#roomTypeId').append(option);
                }
            }
        }
    })
    //根据父级页面判断，如果是修改，则添加默认值
    if (parent.entityType == "toUpdate") {
        $.ajax({
            url: parent.toUpdateURL,
            type: 'post',
            dataType: 'json',
            async: false,//设置阻断
            data: {"id": parent.entityId},
            success: function (ms) {
                // console.log(ms.data);
                var rtid = ms.data.room_type_id;
                // alert(rtid);
                $("#entityId").val(parent.entityId);
                $("#rname").val(ms.data.name);
                $("#roomTypeId").find("option[value=" + rtid + "]").prop("selected", true);
                $("#rsize").val(ms.data.size);
                $("#rtoword").val(ms.data.toword);
            }
        });
    }
    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
            var $ = layui.$
                , form = layui.form;
            form.verify({
                validateMoney: [
                    /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/  //正则表达式
                    , '金额输入有误'  //提示信息
                ],
                mm2: [
                    /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/  //正则表达式
                    , '面积输入有误'  //提示信息
                ]
            });

            //监听提交
            form.on('submit(entity-submit)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                //获取id，若无则改为0
                var id = $("#entityId").val();
                if (id == null || id == "") id = 0;
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: parent.entityURL,
                    type: 'post',
                    dataType: 'json',
                    async: false,//设置阻断
                    data: {
                        "id": id,
                        "name": $("#rname").val(),
                        "room_type_id": $("#roomTypeId").val(),
                        "size": $("#rsize").val(),
                        "toword": $("#rtoword").val()
                    },
                    success: function (data) {
                        parent.layer.msg(data.msg, {icon: data.icon, zIndex: layer.zIndex + 100});
                    }
                });
                parent.layui.table.reload('entityTable'); //重载表格
                parent.layer.close(index); //再执行关闭
            });
        }
    )
</script>
</body>
</html>